<?php

namespace app\index\controller;

use app\BaseController;
use app\index\model\FollowModal;

class UserFollow extends BaseController
{
    protected $middleware = [
        'Auth' => ['except' => ['fans', 'followList']],
        'Openness' => ['only' => ['fans', 'followList']],
    ];
    public function followList()
    {
        $params = $this->request->post(['current', 'size', 'userId']);
        $sql = new FollowModal();
        if(empty($this->request->user)) {
            $currenUserId = 0;
        } else {
            $currenUserId = $this->request->user['id'];
        }
        $data = $sql
            ->where('userid', $params['userId'])
            ->page($params['current'])
            ->limit($params['size'])
            ->with(['fchilder'])
            ->order('create_at', 'desc')
            ->withCount([
                'work'
            ])
            ->select();
        foreach ($data as $key => $value) {
            $gzUser = $value['userinfo'];
            $data[$key]['photoFirst'] = [
                'id' => $gzUser['photoFirst']['id'],
                'name' => $gzUser['photoFirst']['name'],
                'cover' => $gzUser['photoFirst']['cover']
            ];
            $data[$key]['fans_count'] = $sql->where('follow_id', $value['follow_id'])->count();
            if($currenUserId != 0) {
                if($currenUserId == $value['follow_id']) {
                    $data[$key]['isFollow'] = true;
                } else {
                    $data[$key]['isFollow'] = $sql->where('userid', $currenUserId)->where('follow_id', $value['follow_id'])->count() > 0;
                }
            }  else {
                $data[$key]['isFollow'] = false;
            }
            if($data[$key]['isFollow']) {
                if($currenUserId == $value['userid']) {
                    $data[$key]['isByFansConcern'] = true;
                } else {
                    $data[$key]['isByFansConcern'] = $sql->where('userid', $value['follow_id'])->where('follow_id', $currenUserId)->count() > 0;
                }
            }
            foreach ($data[$key]['fchilder'] as $n => $item) {
                $data[$key]['fchilder'][$n] = [
                    'id' => $item['id'],
                    'name' => $item['name'],
                    'cover' => $item['cover'],
                    'book_type' => $item['book_type']
                ];
            }
            $data[$key]['childer'] = $data[$key]['fchilder'];
            unset($data[$key]['fchilder']);
        }
        $total = $sql->where('userid', $params['userId'])->count();
        return result()->success([
            'data' => $data,
            'total' => $total
        ]);
    }
    public function fans()
    {
        $params = $this->request->post(['current', 'size', 'userId']);
        $sql = new FollowModal();
        if(empty($this->request->user)) {
            $currenUserId = 0;
        } else {
            $currenUserId = $this->request->user['id'];
        }
        $data = $sql
            ->where('follow_id', $params['userId'])
            ->page($params['current'])
            ->limit($params['size'])
            ->with(['childer'])
            ->order('create_at', 'desc')
            ->withCount([
                'gzWork'
            ])
            ->select();
        foreach ($data as $key => $value) {
            $gzUser = $value['gzUser'];
            $data[$key]['photoFirst'] = [
                'id' => $gzUser['photoFirst']['id'],
                'name' => $gzUser['photoFirst']['name'],
                'cover' => $gzUser['photoFirst']['cover']
            ];
            $data[$key]['fans_count'] = $sql->where('follow_id', $value['userid'])->count();
            if($currenUserId != 0) {
                if($currenUserId == $value['userid']) {
                    $data[$key]['isFollow'] = true;
                } else {
                    $data[$key]['isFollow'] = $sql->where('userid', $currenUserId)->where('follow_id', $value['userid'])->count() > 0;
                }
            } else {
                $data[$key]['isFollow'] = false;
            }
            if($data[$key]['isFollow']) {
                if($currenUserId == $value['userid']) {
                    $data[$key]['isByFansConcern'] = true;
                } else {
                    $data[$key]['isByFansConcern'] = $sql->where('userid', $value['userid'])->where('follow_id', $currenUserId)->count() > 0;
                }
            }
            foreach ($data[$key]['childer'] as $n => $item) {
                $data[$key]['childer'][$n] = [
                    'id' => $item['id'],
                    'name' => $item['name'],
                    'cover' => $item['cover'],
                    'book_type' => $item['book_type']
                ];
            }
        }
        $total = $sql->where('follow_id', $params['userId'])->count();
        return result()->success([
            'data' => $data,
            'total' => $total
        ]);
    }
    public function list()
    {
        $params = $this->request->post(['current', 'size']);
        $sql = new FollowModal();
        $data = $sql
            ->where('userid', $this->request->user['id'])
            ->page($params['current'])
            ->limit($params['size'])
            ->order('create_at', 'desc')
            ->with('userinfo')
            ->withCount(['follows', 'work'])
            ->select();
        foreach ($data as $key => $value) {
            $data[$key]['photoFirst'] = [
                'id' => $value['userinfo']['photoFirst']['id'],
                'name' => $value['userinfo']['photoFirst']['name'],
                'cover' => $value['userinfo']['photoFirst']['cover']
            ];
        }
        $total = $sql->where('userid', $this->request->user['id'])->count();
        return result()->success([
            'data' => $data,
            'total' => $total
        ]);
    }

    public function add()
    {
        $params = $this->request->post(['userid']);
        $sql = new FollowModal();
        $data = [
            'userid' => $this->request->user['id'],
            'follow_id' => $params['userid']
        ];
        $res = $sql->where($data)->find();
        if($res) {
            return result()->fail("已关注", 500);
        }
        $sql->save($data);
        $count = $sql->where('follow_id', $this->request->user['id'])->where('userid', $params['userid'])->count();
        return result()->success($count > 0);
    }
    public function cancel()
    {
        $params = $this->request->post(['userid']);
        $sql = new FollowModal();
        $data = [
            'userid' => $this->request->user['id'],
            'follow_id' => $params['userid']
        ];
        $res = $sql->where($data)->find();
        if(!$res) {
            return result()->fail("未关注", 500);
        }
        $sql->where($data)->delete();
        return result()->onlySucMsg("取消关注成功");
    }

}